﻿using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms.VisualStyles;


#region 文件信息
/****************************************************************
*	公司名称：福建力和行智能科技有限公司
*   作    者：Jeff
*   CLR 版本：4.0.30319.42000
*   创建时间：12/20/2024 11:17:59 AM
*	用 户 名：zheqichen 
*   描述说明：
*
*   修改历史：
*		1）	修 改 人：
*			修改日期：
*			修改内容：			
*
*****************************************************************/
#endregion

namespace Lhxzn.LangExtractor
{
    /// <summary>
    /// 类    名:TreeDataGridViewTextBoxCell
    /// 描    述:
    /// 修改时间:12/20/2024 11:17:59 AM
    /// </summary>
    public class TreeDataGridViewTextBoxCell: DataGridViewTextBoxCell
    {
        #region 成员变量

        #region private

        /// <summary>
        /// 收缩按钮在cell中的位置
        /// </summary>
        private Rectangle m_rectCollapse;

        /// <summary>
        /// 选中按键的位置
        /// </summary>
        private Rectangle m_rectCheckBox;

        #endregion


        #region protected



        #endregion


        #region public

        public const int LineSpacing = 14;

        public const int CheckBoxSize = 14;


        public TreeDataGridViewRow OwningTreeRow => OwningRow as TreeDataGridViewRow;

        #endregion

        #endregion


        #region 构造函数
        /// <summary>
        /// 函 数 名:构造函数
        /// 函数描述:默认构造函数
        /// 修改时间:12/20/2024 11:17:59 AM
        /// </summary>
        public TreeDataGridViewTextBoxCell()
        {

        }
        #endregion

        #region 父类函数重载、接口实现
        protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
        {
            base.OnMouseClick(e);

            if (e.Button == MouseButtons.Left)
            {
                if (m_rectCollapse.Contains(e.Location))
                {
                    OwningTreeRow.IsCollapse = !OwningTreeRow.IsCollapse;
                }
                else if (m_rectCheckBox.Contains(e.Location))
                {
                    OwningTreeRow.Checked =
                        OwningTreeRow.Checked == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked;
                }
            }
        }

        protected override void OnMouseDoubleClick(DataGridViewCellMouseEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            (this.OwningRow as TreeDataGridViewRow).IsCollapse =
                 !(this.OwningRow as TreeDataGridViewRow).IsCollapse;
        }

        protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)
        {
            cellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            Brush brush;
            if ((cellState & DataGridViewElementStates.Selected) > 0)
            {
                brush = new SolidBrush(cellStyle.SelectionBackColor);
            }
            else
            {
                brush = new SolidBrush(cellStyle.BackColor);
            }

            graphics.FillRectangle(brush, cellBounds);

            if (this.OwningRow as TreeDataGridViewRow != null && this.ColumnIndex >= 0 && this.RowIndex >= 0)
            {
                TreeDataGridViewRow treeRow = (TreeDataGridViewRow)this.OwningRow;
                this.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);

                Pen pen = new Pen(this.DataGridView.GridColor);
                pen.Width = 1f;
                pen.DashStyle = DashStyle.Dot;
                int level = treeRow.Level;

                int line_x = 0;
                int line_x_end = line_x + LineSpacing;

                #region 画虚线
                for (int i = 1; i <= level + 1; i++)
                {
                    line_x = cellBounds.Left + i * LineSpacing;
                    line_x_end = line_x + LineSpacing;

                    //画竖着的虚线
                    graphics.DrawLine(pen, line_x, cellBounds.Top, line_x, cellBounds.Bottom);

                }
                int draw_width = (level + 2) * LineSpacing;

                int line_y = (cellBounds.Top + cellBounds.Bottom) / 2;

                //画横着的线
                graphics.DrawLine(pen, line_x, line_y, line_x_end, line_y);

                #endregion

                #region 画收缩的按钮
                //画收缩按钮
                if (treeRow.Rows.Count > 0)
                {
                    int btnSize = LineSpacing;

                    Rectangle CollapseRect = new Rectangle(line_x - btnSize / 2, line_y - btnSize / 2, btnSize, btnSize);
                    ButtonRenderer.DrawButton(graphics,
                       CollapseRect,
                        treeRow.IsCollapse ? "+" : "-",
                         new Font("宋体", btnSize * 0.75f), false, System.Windows.Forms.VisualStyles.PushButtonState.Normal);
                    m_rectCollapse = new Rectangle(
                        new Point(CollapseRect.X - cellBounds.Left, CollapseRect.Y - cellBounds.Top), CollapseRect.Size);
                }
                else
                {
                    m_rectCollapse = new Rectangle(0, 0, 0, 0);
                }

                #endregion


                #region 画选择框

                line_x = line_x_end;

                if (treeRow.Checked != CheckState.Indeterminate)
                {
                    CheckBoxRenderer.DrawCheckBox(graphics,
                        new Point(line_x, (cellBounds.Height - CheckBoxSize) / 2 + cellBounds.Top),
                      treeRow.Checked == CheckState.Checked ? CheckBoxState.CheckedNormal : CheckBoxState.UncheckedNormal);

                    m_rectCheckBox = new Rectangle(line_x - cellBounds.Left,
                        (cellBounds.Height - CheckBoxSize) / 2, CheckBoxSize, CheckBoxSize);
                    line_x += CheckBoxSize;
                    draw_width += CheckBoxSize;
                }
                else
                {
                    m_rectCheckBox = new Rectangle(0, 0, 0, 0);
                }
                #endregion

                cellBounds = new Rectangle(line_x, cellBounds.Top, cellBounds.Width - draw_width, cellBounds.Height);

            }
            else
            {
                cellBounds = new Rectangle(cellBounds.Left + LineSpacing, cellBounds.Top, cellBounds.Width - LineSpacing, cellBounds.Height);

            }


            advancedBorderStyle.Left = DataGridViewAdvancedCellBorderStyle.None;
            base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts);


        }
        #endregion

        #region 函数

        #region private



        #endregion


        #region protected



        #endregion


        #region public


        #endregion

        #endregion
    }
}
